<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<title>管理中心</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="/sys-admin/css/general.css" rel="stylesheet" type="text/css">
<link href="/sys-admin/css/main.css" rel="stylesheet" type="text/css">
<script src="/sys-admin/js/jquery.js" type="text/javascript"></script>
<script src="/sys-admin/js/cn_message.js" type="text/javascript"></script>
<script src="/sys-admin/js/validate.js" type="text/javascript"></script>
<script src="/sys-admin/js/jquery.form.js" type="text/javascript"></script>
<script src="/sys-admin/js/common.js" type="text/javascript"></script>
</head>
<body>
<h1>
<span class="action-span"><a href="<{$back_url}>">返回</a></span>
<span id="search_id" class="action-span1">产品内容管理</span>
</h1>
<div class="list-div" id="listDiv">
<table cellpadding="3" cellspacing="1" id="s_table">
  <tr>
    <th class="num">#</th>
    <th>名称</th>
    <th>图片</th>
    <th>标题</th>
    <th>所选产品</th>
    <th>排序</th>
    <th>操作</th>
  </tr>
   <{foreach from=$stores item=store name=i}>
    <tr id="l_<{$store.id}>">
	<td><{$smarty.foreach.i.index+1}></td>
	<td><{$store.name}></td>
	<td><img src="<{$store.src}>" class="store_list_img"/></td>
	<td><{$store.title}></td>
	<td>asdadasd</td>
	<td><{$store.sort_order}></td>
	<td>
	<a href="javascript:delProductStore(<{$store.id}>);">删除</a>
	<a href="javascript:popEditProductStoreImg(<{$store.id}>);">修改图片</a>
	<a href="javascript:popEditProductStore(<{$store.id}>);">修改</a>
	</td>
    </tr>
   <{/foreach}>
</table>
</div>
<br/>
<div class="list-div">
<form action="/sys-admin/action/cms/store.php" enctype="multipart/form-data" method="post" id="add_store">
<input type="hidden" value="<{$block.block_id}>" name="blockid" id="b_id"/>
<input type="hidden" name="met" value="addproduct"/>
<input type="hidden" name="sproid" value="" id="product_id"/>
<table>
 <tr>
    <td class="label"><span class="require-field">*</span>选择商品</td>
	<td>
	<div>
		<select id="sel_cat_id">
		<option value="0">所有分类</option>
		<{foreach from=$categorys item=child0}>
			<optgroup label="<{$child0.cat_name}>">
			<{if $child0.childs|@count gt 0}>
			  	<{foreach from=$child0.childs item=child1}>
			  		<optgroup label="<{$child1.cat_name}>">
			  		<{if $child1.childs|@count gt 0}>
			  	 		<{foreach from=$child1.childs item=child2}>
			  			<option value="<{$child2.cat_id}>"><{$child2.cat_name}></option>
			  			<{/foreach}>
			  		<{/if}>
			  		</optgroup> 
			  	<{/foreach}>
		  	<{/if}>
		  	</optgroup>  
		<{/foreach}> 
		</select>
		<input type="text" id="key"/>
		<input class="button" type="button" onclick="searchProduct2Store();" value=" 搜索 ">
	</div>
	<div class="sel_product_list" id="sel_product_list"></div>
	<div>当前选择：<b id="cur_sel_product"></b></div>
	</td>
  </tr>
 <tr>
    <td class="label"><span class="require-field">*</span>名称</td>
    <td><input type="text" name="name" maxlength="60" size="20" id="product_name"/></td>
 </tr>
 <tr>
    <td class="label"><span class="require-field">*</span>图片</td>
    <td><input type="file" name="image" id="product_img"/><span>只能是jpg、gif、png,小于1M</span></td>
 </tr>
 <tr>
    <td class="label">标题</td>
    <td><input type="text" name="title" maxlength="60" size="20" id="product_title"/></td>
 </tr>
 <tr>
    <td class="label">排序</td>
    <td><input type="text" maxlength="2" name="sort" value="1" size="4" id="product_sort" vtype="not-required-n" vname="sort_order"/><span id="sort_order_m"></span></td>
  </tr>
 <tr>
    <td class="label"></td>
    <td>
      <input type="button" value="添加 " class="button" onclick="addProductStore();"/>&nbsp;&nbsp;&nbsp;
   </td>
  </tr>
  </table>
</form>
</div>
<script type="text/javascript">

function addProductStore(){
	if($('#product_id').val()=='' || parseInt($('#product_id').val()) < 1){
		alert('请选择产品');
		return;
	}
	if(!Fun.isEmpty($('#product_name'))){
		alert('名称不能为空');
		return;
	}
	if(!Fun.isEmpty($('#product_img'))){
		alert('请选择图片');
		return;
	}
	$('#add_store').ajaxSubmit({
		success:function(result){
			result = $.trim(result);
			var json = eval('('+result+')');
			if(json.code == 1){
				var name =$.trim($('#product_name').val());
				var title =$.trim($('#product_title').val());
				var sort =parseInt($('#product_sort').val());
				sort = sort>0?sort:1;
				var html = '<tr id="l_'+json.id+'"><td>-</td>'
				    +'<td>'+name+'</td>'
				    +'<td><img src="'+json.src+'" style="height: auto;max-height: 100px;max-width: 100px;width: auto;vertical-align: middle;"/></td>'
				    +'<td>'+title+'</td>'
				    +'<td>'+$('#cur_sel_product').html()+'</td>'
				    +'<td>'+sort+'</td>'
				    +'<td><a href="javascript:delProductStore('+json.id+');">删除</a>'
				    +'<a href="javascript:popEditProductStoreImg('+json.id+');">修改图片</a>'
				    +'<a href="javascript:popEditProductStore('+json.id+');">修改</a></td></tr>';
				$('#s_table').append(html);
				$('#product_name').val('');
				$('#product_title').val('');
				$('#product_sort').val(1);
				$('#product_id').val('');
				$('#cur_sel_product').html('');
				alert('添加成功');
			}else{
				alert('添加失败');
			}
		}
	});
}


function popEditProductStoreImg(id){
	var html ='<form action="/sys-admin/action/cms/store.php" enctype="multipart/form-data" method="post" id="edit_store_img">'
		+'<input type="hidden" name="met" value="editproductimg"/>'
		+'<input type="hidden" value="'+id+'" name="id" id="pop_id"/>'
		+'图片：<input type="file" name="image" id="pop_img"/><span>只能是jpg、gif、png,小于1M</span>' 	
		+'<br/><input class="button" type="button" value="修改" onclick="editProductStoreImg('+id+');">'
	    +'</form>';
		popDailog('修改图片',html,160);
}

function editProductStoreImg(id){
	if(!Fun.isEmpty($('#pop_img'))){
		alert('请选择图片');
		return;
	}
	$('#edit_store_img').ajaxSubmit({
		success:function(result){
			result = $.trim(result);
			var json = eval('('+result+')');
			if(json.code == 1){
				$('#l_'+id).find('td').eq(2).find('img').attr('src',json.src);
				closeUpImg();
				alert('图片修改成功');
			}else{
				alert('图片修改失败');
			}
		}
	});
}

function popEditProductStore(id){
	var name = $.trim($('#l_'+id).find('td').eq(1).html());
	var title = $.trim($('#l_'+id).find('td').eq(3).html());
	var sort = $.trim($('#l_'+id).find('td').eq(5).html());
	var html ='<form action="/sys-admin/action/cms/store.php" method="post" id="edit_store">'
		+'<input type="hidden" name="met" value="editproduct"/>'
		+'<input type="hidden" value="'+id+'" name="id" id="pop_id"/>'
		+'名称：<input type="text" name="name" maxlength="60" value="'+name+'" size="20" id="pop_name"/>'
		+'<p>标题：<input type="text" name="title" maxlength="60" size="20" value="'+title+'" id="pop_title"/></p>' 	
		+'<p>排序：<input type="text" maxlength="2" name="sort" value="1" size="4" value="'+sort+'" id="pop_sort"></p>' 	
		+ '<input class="button" type="button" value="修改" onclick="editProductStore('+id+');">'
	    +'</form>';
		popDailog('修改数据',html,200);
}

function editProductStore(id){
	if(!Fun.isEmpty($('#pop_name'))){
		alert('名称不能为空');
		return;
	}
	if(!Fun.zIntCheck($('#pop_sort'))){
		alert('排序必须为整数');
		return;
	}
	$('#edit_store').ajaxSubmit({
		success:function(result){
			result = $.trim(result);
			var json = eval('('+result+')');
			if(json.code == 1){
				var name =$.trim($('#pop_name').val());
				var title =$.trim($('#pop_title').val());
				var sort =parseInt($('#pop_sort').val());
				sort = sort>0?sort:1;
				$('#l_'+id).find('td').eq(1).html(name);
				$('#l_'+id).find('td').eq(3).html(title);
				$('#l_'+id).find('td').eq(5).html(sort);
				closeUpImg();
				alert('修改成功');
			}else{
				alert('修改失败');
			}
		}
	});
}

function delProductStore(id){
	var name = $('#l_'+id).find('td').eq(1).html();
	if(confirm('确定要删除'+name+'?')){
		var url = '/sys-admin/action/cms/store.php?met=delproduct'
		       +'&id='+id
		       +'&t='+(new Date().getTime());
		$.get(url,null,function(result){
			if($.trim(result) == "1"){
				$('#l_'+id).remove();
				alert("删除成功");
			}else{
				alert("删除失败");
			}
		});
	}
}

function searchProduct2Store(){
	$('#sel_product_list').html('');
	var key = $.trim($('#key').val());
	var catid = parseInt($('#sel_cat_id').val());
	var url = '/sys-admin/action/goods/product.php?met=storeproductlist'
	       +'&key='+key
	       +'&t='+(new Date().getTime());
	if(catid > 0){
		url += '&catid='+catid
	}
    $.get(url,null,function(result){
    	var json = eval('('+$.trim(result)+')');
    	var html = '';
    	for(var i=0;i<json.length;i++){
    		html += '<ul id="pl_'+json[i].id+'"><li class="wp40">'+json[i].name+'</li><li class="wp20">'+json[i].attr+'</li><li class="wp20">15.00</li><li><a href="javascript:selectProduct2Store('+json[i].id+');">选择</a></li></ul>';
    	}
    	$('#sel_product_list').html(html);
	});
}
function selectProduct2Store(id){
	if(id > 0){
		var name = $('#pl_'+id).find('li').eq(0).html();
		var attr = $('#pl_'+id).find('li').eq(1).html();
		$('#cur_sel_product').html(name+'  '+attr);
		$('#product_name').val(name);
		$('#product_title').val(name);
		$('#product_id').val(id);
	}
}



</script>
</body></html>